﻿using System;
using System.Collections.Generic;
using System.Linq;
using GCTrackableRace.Interfaces;
using GcTrackableRace.Entities;
using GcTrackableRace.ScoreCalculator.Helpers;

namespace GcTrackableRace.ScoreCalculator.Rules
{
	public class CountAllLogs: BaseRule, IRaceRule
	{
		public CountAllLogs(string reportColumnName, string startDate, string endDate, string pointAllocation)
			:base(reportColumnName, startDate, endDate, pointAllocation)
		{}
  
		protected override void SetRuleName()
		{
			RuleName = "All Logs";
		}

		public override void Process(ITrackable trackable, IEnumerable<ITrackableLog> trackableLogs)
		{
			var applicableLogs = (
				from log in trackableLogs
				where (
					(log.LogType == LogType.DroppedOff || log.LogType == LogType.Visited)
					&&
					log.Date >= StartDate 
					&&
					log.Date <= EndDate
				)
				select log)
				.OrderBy(logs => logs.Date)
				.ThenBy(logs => logs.LogId)
				.ToList();

			trackable.AddScore(new RuleScore(
				RuleName, 
				ReportColumnName,
				applicableLogs.Count,
				(int)(applicableLogs.Count * PointAllocation),
				applicableLogs.Count.ToString())
			);
		}
	}
}